package app.framework.security.authorization;

import app.framework.front.handler.FrontHandler;
import app.framework.front.interceptor.FrontInterceptor;
import app.framework.front.util.PageConfig;
import app.framework.front.util.WebUtil;
import app.q.StringUtil;
import jasmine.framework.context.RuntimeProvider;
import jasmine.framework.web.util.routing.Route;
import jasmine.security.strategy.DefaultUrlPatternMatcher;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 支持匹配前端资源 URL。
 * </p>
 *
 * @author mh.z
 */
public class FrontPageSupportUrlPatternMatcher extends DefaultUrlPatternMatcher {
    private FrontInterceptor frontInterceptor;

    @Override
    public void init(RuntimeProvider provider) {
        super.init(provider);
        this.frontInterceptor = provider.getByType(FrontInterceptor.class);
    }

    @Override
    public String getUrlPattern(HttpServletRequest request) {
        String originalUrl = WebUtil.getRequestUriWithoutContextPath(request);

        // 优先尝试获取前端的路由
        FrontHandler frontHandler = frontInterceptor.getFrontHandler();

        String urlPattern = frontHandler.getUrlPattern(originalUrl);
        if (urlPattern != null) {
            return urlPattern;
        }

        urlPattern = super.getUrlPattern(request);
        if (urlPattern != null) {
            return urlPattern;
        }

        // 未找到 URL 模式则返回原 URL
        return originalUrl;
    }

}
